﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpWeb
{
    /// <summary>
    /// input标签
    /// </summary>
    public class InputTag : TagComponent, IFrontValidationProvider
    {
        #region 属性
        protected override string TagName
        {
            get { return "input"; }
        }

        protected override bool CloseItSelf
        {
            get { return true; }
        }
        private ValidationSetting _ValidationSetting;
        /// <summary>
        /// 验证设置
        /// </summary>
        public ValidationSetting ValidationSetting
        {
            get
            {
                if (_ValidationSetting == null)
                    _ValidationSetting = new ValidationSetting();
                return _ValidationSetting;
            }
        }

        /// <summary>
        /// 规定通过文件上传来提交的文件的类型。
        /// </summary>
        public string accept
        {
            get { return this.Attributes["accept"]; }
            set { this.Attributes["accept"] = value; }
        }

        /// <summary>
        /// 定义输入的替代文本
        /// </summary>
        public string alt
        {
            get { return this.Attributes["alt"]; }
            set { this.Attributes["alt"] = value; }
        }

        /// <summary>
        /// 规定此 input 元素首次加载时应当被选中。
        /// </summary>
        public string @checked
        {
            get { return this.Attributes["checked"]; }
            set { this.Attributes["checked"] = value; }
        }
        /// <summary>
        /// 当 input 元素加载时禁用此元素。
        /// </summary>
        public string disabled
        {
            get { return this.Attributes["disabled"]; }
            set { this.Attributes["disabled"] = value; }
        }
        /// <summary>
        /// 规定输入字段中的字符的最大长度。
        /// </summary>
        public string maxlength
        {
            get { return this.Attributes["maxlength"]; }
            set { this.Attributes["maxlength"] = value; }
        }

        /// <summary>
        /// 规定输入字段为只读。
        /// </summary>
        public string @readonly
        {
            get { return this.Attributes["readonly"]; }
            set { this.Attributes["readonly"] = value; }
        }
        /// <summary>
        /// 定义输入字段的宽度。
        /// </summary>
        public string size
        {
            get { return this.Attributes["size"]; }
            set { this.Attributes["size"] = value; }
        }

        /// <summary>
        /// 定义以提交按钮形式显示的图像的 URL。
        /// </summary>
        public string src
        {
            get { return this.Attributes["src"]; }
            set { this.Attributes["src"] = value; }
        }


        public InputTagType type
        {
            get { return (InputTagType)Enum.Parse(typeof(InputTagType), this.Attributes["type"]); }
            set { this.Attributes["type"] = Enum.GetName(typeof(InputTagType), value); }
        }
        /// <summary>
        /// 规定 input 元素的值。
        /// </summary>
        public string value
        {
            get { return this.Attributes["value"]; }
            set { this.Attributes["value"] = value; }
        }

        /// <summary>
        /// 设置元素的 Tab 键控制次序。
        /// </summary>
        public string tabindex
        {
            get { return this.Attributes["tabindex"]; }
            set { this.Attributes["tabindex"] = value; }
        }

        #endregion

        #region 方法

        protected override void OnRenderBegining(StringBuilder sb, ComponentContext context)
        {
            if (this._ValidationSetting != null)//此处判断字段是否为空，避免访问属性时生成空实例。
            {
                this.ValidationSetting.Apply(this);//将设置应用到当前标签
            }
            base.OnRenderBegining(sb, context);
        }
        #endregion
    }
    /// <summary>
    /// 输入标签的类型
    /// </summary>
    public enum InputTagType
    {
        text = 0,
        button,
        checkbox,
        file,
        hidden,
        image,
        password,
        radio,
        reset,
        submit
    }
}
